﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;

namespace Web.Page
{
    public partial class departmentUsersTreeView : System.Web.UI.Page
    {
        Departments_Dao dao = new Departments_Dao();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                #region 节点
                Department dept = dao.GetRoot();
                if (dept == null)
                {
                    return;
                }
                TreeNode root = new TreeNode();
                root.Text = dept.Name;
                root.Value = dept.Id.ToString();
                root.PopulateOnDemand = true;
                //root.NavigateUrl = string.Format("DepartmentEdit.aspx?id={0}", root.Value);
                root.ImageUrl = "../images/user/group.png";
                tvOrg.Nodes.Add(root);
                #endregion
            }
        }

        private void LoadChildNode(TreeNode node)
        {
            List<Department> ds = dao.GetChildren(node.Value);
            foreach (Department dept in ds)
            {
                TreeNode child = new TreeNode();
                child.Text = dept.Name;
                child.Value = dept.Id.ToString();
                child.PopulateOnDemand = true;
                //child.NavigateUrl = string.Format("DepartmentEdit.aspx?id={0}", child.Value);
                child.ImageUrl = "../images/user/group.png";

                node.ChildNodes.Add(child);
            }
        }

        private void LoadUsers(TreeNode node)
        {
            List<User> us = dao.GetUsers(node.Value);
            foreach (User u in us)
            {
                TreeNode child = new TreeNode();
                child.Text = u.Name;
                child.Value = u.Id.ToString();
                child.PopulateOnDemand = true;
                //child.NavigateUrl = string.Format("UserEdit.aspx?id={0}", child.Value);
                child.ImageUrl = "../images/user/user.png";
                child.Expanded = true;
                node.ChildNodes.Add(child);
            }
        }

        protected void tvOrg_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            //if (IsCallback)
            //{
                if (e.Node.ChildNodes.Count == 0)
                {
                    LoadChildNode(e.Node);
                    LoadUsers(e.Node);
                }
            //}
        }

        protected void tvOrg_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (tvOrg.SelectedNode.ChildNodes.Count == 0)
            {
                Request.QueryString["users"] = tvOrg.SelectedNode.Text;
                Request.QueryString["usersID"]  = tvOrg.SelectedNode.Value;                
                Request.QueryString["departments"]  = tvOrg.SelectedNode.Parent.Text;
                Request.QueryString["departmentsID"]  = tvOrg.SelectedNode.Parent.Value;
            }
        }

    }
}